package controller;

import java.security.InvalidParameterException;
import java.util.Collection;

import model.Time;

/**
 * @author samuel
 * 
 */
public class LightPeriodValidator implements Validator<Time> {

	private Time LOWER_PERIOD_BOUND = Time.makeTimeFromSecond(10);
	private Time UPPER_PERIOD_BOUND = Time.makeTimeFromSecond(60);
	private static int MAXIMUM_LIGHTS = 50;
	private static int MINIMUM_LIGHTS = 1;

	/**
	 * @param period
	 * @return
	 */
	public boolean isValid(Time period) {
		boolean isWithinBound;
		boolean isAboveOrEqualToLowerLimit = period.compare(LOWER_PERIOD_BOUND) >= 0;
		boolean isUnderOrEqualToUpperLimit = period.compare(UPPER_PERIOD_BOUND) <= 0;

		isWithinBound = isAboveOrEqualToLowerLimit
				&& isUnderOrEqualToUpperLimit;

		boolean isInteger = period.getTimeInSecond() == period
				.getFloredTimeInSecond();

		return isWithinBound && isInteger;

	}

	/**
	 * @param period
	 */
	public void validate(Time period) {

		if (!isValid(period)) {
			String errorMessage = "The folowing period "
					+ period.getFloredTimeInSecond()
					+ " does not meet the requirement, the period must be within the folowing limits ["
					+ LOWER_PERIOD_BOUND + "," + UPPER_PERIOD_BOUND + "]";

			throw new InvalidParameterException(errorMessage);
		}
	}

	/**
	 * Throws InvalidParameterException
	 * 
	 * @param periods
	 */
	public void validate(Collection<Time> periods) {

		int size = periods.size();

		if (size < MINIMUM_LIGHTS || size > MAXIMUM_LIGHTS) {
			String errorMessage = "Given periods number ar not withing boundry: list size("
					+ size
					+ ") The number of traffic lights have to be between ["
					+ MINIMUM_LIGHTS + "," + MAXIMUM_LIGHTS + "]";

			throw new InvalidParameterException(errorMessage);
		} else {
			for (Time time : periods) {
				validate(time);
			}
		}
	}
}
